package com.cn.springcloudwechat.springUtil;

import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.logging.log4j.util.Strings;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * feigin调用业务时保持session一致
 */
@Configuration
@EnableFeignClients(basePackages = {"com.cn"})
public class FeiginConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return;
        }

        String sessionId = ((ServletRequestAttributes) requestAttributes).getRequest().getSession().getId().toString();
        if(!Strings.isNotBlank(sessionId)){
            requestTemplate.header("Cookie", "SESSION=" + sessionId);
        }
    }
}
